#include <gtest/gtest.h>
#include "UdpSocketWrapper.hpp"
#include "ParserPacket.hpp"
#include "RtpPacket.hpp"
#include <arpa/inet.h>
#include <sstream>
#include "RTPDecoderHandle.hpp"
#include "RTPDecoderWrapper.hpp"
#include "HiRtpCommonUtil.h"

static const char* myIp = "127.0.0.1";

using namespace Hi;
using namespace std;
static Hi::RTPDecoderWrapper decoder;

static uint32_t s_index = 0;
class MyRTPDecoderHandle: public Hi::RTPDecoderHandle {
public:
	void OnYUV(const uint8_t* data, size_t len, uint32_t width, uint32_t height, void*) {
		printf("OnYUV\n");
	}
	void OnRGB(const uint8_t* data, size_t len, uint32_t width, uint32_t height, void*) {
		printf("OnRGB\n");
		stringstream oss;
			oss<<"./bmp/"<<s_index % 100<<"_cpp_dec.bmp";
		hi_saveBmp(oss.str().c_str(), data, width, height);
		s_index++;
	}
};

static uint16_t  seq = 0;
static void UdpReceiveMsg(const uint8_t* data, size_t len, 
    const struct sockaddr* addr, UdpSocketWrapper* wrapper) {
    RtpPacket* packet = hi_RtpPacket_parse(data, len);
    if (packet == nullptr) {
		return;
    } 
	
	if (seq + 1 != packet->GetSequenceNumber()) {
		printf("packet seq:%d, last seq:%d\n", packet->GetSequenceNumber(), seq);
	}	
	seq = packet->GetSequenceNumber();
	decoder.Decode(packet);
	hi_RtpPacket_release(&packet);
}

TEST(RTPDecorder, decoderRTSP) {	
	return;	
    system("rm -fr bmp/*");
	s_index = 0;
	MyRTPDecoderHandle handle;
	decoder.Start(handle, 1024, 576, nullptr);
    UdpSocketWrapper* udp = new UdpSocketWrapper();
	int ret = udp->Listen(myIp, 60001, UdpReceiveMsg);
	EXPECT_EQ(ret, 0);
	sleep(1000000); 
}